#include "../include/CacheManager.h"

pthread_once_t CacheManager::_ptht = PTHREAD_ONCE_INIT;

CacheManager::CacheManager(){}

CacheManager* CacheManager::_pinstance = nullptr;

void CacheManager::initCache(size_t num, const string &filename)
{
    if(num<0)
        return;
    for (int i = 0; i < num;i++)
    {
        _cacheList.emplace_back(num);
        if(filename!=string())
        {
            _cacheList[i].readFromFile(filename);
        }
    }
}

LRUCache &CacheManager::getCache(size_t idx)
{
    return _cacheList[idx];
}


//定时更新缓存，将所有的cache的pendinglist的结点依次插入主Cache中

void CacheManager::updateAllCaches(){
    int size = _cacheList.size();
    LRUCache &Lrucache = _cacheList[0];
    for (int i = 1; i < size;++i)
    {
        LRUCache &temp = _cacheList[i];
        if(temp._keyMap.size()==0)
        {
            cout << "List NOTHING!" << endl;
            continue;
        }
        Lrucache.mergePending(temp);
    }
    for (int i = 1; i < size; ++i)
    {
        LRUCache &temp = _cacheList[i];
        temp.update(Lrucache);
    }
}

